{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import os\n",
    "import matplotlib.font_manager as fm\n",
    "import scipy.stats as stats\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "import scipy\n",
    "from docx import Document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#读取预处理过的数据\n",
    "original_Single = pd.read_excel('./results/销售量按单品-月统计.xlsx')\n",
    "original_Catagory = pd.read_excel('./results/销售量按分类-月统计.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 1 列数据满足正态分布\n",
      "第 2 列数据满足正态分布\n",
      "第 3 列数据满足正态分布\n",
      "第 4 列数据满足正态分布\n",
      "第 5 列数据满足正态分布\n",
      "第 6 列数据满足正态分布\n",
      "JB检验结果(h): [2.105402 0.910489 1.798564 0.576506 3.712456 2.787277]\n",
      "JB检验结果(p): [0.348994 0.634293 0.406862 0.749572 0.156261 0.248171]\n"
     ]
    }
   ],
   "source": [
    "# 进行分类的正态分布检验\n",
    "\n",
    "# 提取需要检验的列数据\n",
    "column_data  = original_Catagory[['水生根茎类', '花叶类', '花菜类', '茄类', '辣椒类', '食用菌']]\n",
    "alpha = 0.05\n",
    "n_c = column_data.shape[1]\n",
    "H = np.zeros(n_c)\n",
    "P = np.zeros(n_c)\n",
    "\n",
    "for i in range(column_data.shape[1]):\n",
    "    h, p = stats.jarque_bera(column_data.iloc[:, i])\n",
    "    H[i] = h\n",
    "    P[i] = p\n",
    "    \n",
    "    if p < alpha:\n",
    "        print(\"第\", i+1, \"列数据不满足正态分布\")\n",
    "    else:\n",
    "        print(\"第\", i+1, \"列数据满足正态分布\")\n",
    "\n",
    "print(\"JB检验结果(h):\", H)\n",
    "print(\"JB检验结果(p):\", P)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 进行蔬菜单品的正态分布检验\n",
    "\n",
    "# 提取需要检验的列数据\n",
    "column_data  = original_Single[['七彩椒(1)', '七彩椒(2)', '七彩椒(份)', '上海青', '上海青(份)', '东门口小白菜', '丝瓜尖', '云南油麦菜', '云南油麦菜(份)', '云南生菜', '云南生菜(份)', '余干椒', '保康高山大白菜', '冰草', '冰草(盒)', '净藕(1)', '净藕(2)', '净藕(3)', '南瓜尖', '双孢菇', '双孢菇(份)', '双孢菇(盒)', '双沟白菜', '和丰阳光海鲜菇(包)', '四川红香椿', '圆茄子(1)', '圆茄子(2)', '外地茼蒿', '外地茼蒿(份)', '大白菜', '大白菜秧', '大芥兰', '大龙茄子', '奶白菜', '奶白菜(份)', '奶白菜苗', '姜蒜小米椒组合装(小份)', '姬菇(1)', '姬菇(2)', '姬菇(份)', '姬菇(包)', '娃娃菜', '小白菜', '小白菜(份)', '小皱皮', '小皱皮(份)', '小米椒', '小米椒(份)', '小青菜(1)', '小青菜(2)', '小青菜(份)', '平菇', '快菜', '春菜', '木耳菜', '木耳菜(份)', '本地上海青', '本地小毛白菜', '本地黄心油菜', '杏鲍菇(1)', '杏鲍菇(2)', '杏鲍菇(250克)', '杏鲍菇(份)', '杏鲍菇(袋)', '枝江红菜苔', '枝江红菜苔(份)', '枝江青梗散花', '槐花', '水果辣椒', '水果辣椒(份)', '水果辣椒(橙色)', '油菜苔', '泡泡椒(精品)', '洪山菜苔', '洪山菜薹珍品手提袋', '洪山菜薹莲藕拼装礼盒', '洪湖莲藕(粉藕)', '洪湖莲藕(脆藕)', '洪湖藕带', '活体银耳', '海鲜菇(1)', '海鲜菇(2)', '海鲜菇(份)', '海鲜菇(包)', '海鲜菇(袋)(1)', '海鲜菇(袋)(2)', '海鲜菇(袋)(3)', '海鲜菇(袋)(4)', '灯笼椒(1)', '灯笼椒(2)', '灯笼椒(份)', '牛排菇', '牛排菇(盒)', '牛首油菜', '牛首生菜', '猪肚菇(盒)', '猴头菇', '甘蓝叶', '甜白菜', '田七', '白玉菇(1)', '白玉菇(2)', '白玉菇(盒)', '白玉菇(袋)', '白菜苔', '白蒿', '秀珍菇', '竹叶菜', '竹叶菜(份)', '紫圆茄', '紫尖椒', '紫白菜(1)', '紫白菜(2)', '紫苏', '紫苏(份)', '紫茄子(1)', '紫茄子(2)', '紫螺丝椒', '紫贝菜', '红尖椒', '红尖椒(份)', '红杭椒', '红杭椒(份)', '红椒(1)', '红椒(2)', '红椒(份)', '红橡叶', '红灯笼椒(1)', '红灯笼椒(2)', '红灯笼椒(份)', '红珊瑚(粗叶)', '红线椒', '红莲藕带', '红薯尖', '红薯尖(份)', '组合椒系列', '绣球菌', '绣球菌(袋)', '绿牛油', '艾蒿', '芜湖青椒(1)', '芝麻苋菜', '芥兰', '芥菜', '花茄子', '花菇(一人份)', '苋菜', '苋菜(份)', '茶树菇(袋)', '茼蒿', '茼蒿(份)', '荠菜', '荸荠', '荸荠(份)', '莲蓬(个)', '菊花油菜', '菌菇火锅套餐(份)', '菌蔬四宝(份)', '菜心', '菜心(份)', '菠菜', '菠菜(份)', '菱角', '萝卜叶', '蒲公英', '蔡甸藜蒿', '蔡甸藜蒿(份)', '薄荷叶', '藕尖', '虫草花', '虫草花(份)', '虫草花(盒)(2)', '虫草花(袋)', '螺丝椒', '螺丝椒(份)', '蟹味菇(1)', '蟹味菇(2)', '蟹味菇(盒)', '蟹味菇(袋)', '蟹味菇与白玉菇双拼(盒)', '襄甜红菜苔(袋)', '西兰花', '西峡花菇(1)', '西峡花菇(2)', '西峡香菇(1)', '西峡香菇(2)', '西峡香菇(份) ', '豌豆尖', '赤松茸', '赤松茸(盒)', '辣妹子', '野生粉藕', '野藕(1)', '野藕(2)', '金针菇(1)', '金针菇(2)', '金针菇(份)', '金针菇(盒)', '金针菇(袋)(1)', '金针菇(袋)(2)', '金针菇(袋)(3)', '银耳(朵)', '长线茄', '随州泡泡青', '青尖椒', '青尖椒(份)', '青杭椒(1)', '青杭椒(2)', '青杭椒(份)', '青梗散花', '青红尖椒组合装(份)', '青红杭椒组合装(份)', '青线椒', '青线椒(份)', '青茄子(1)', '青茄子(2)', '青菜苔', '面条菜', '马兰头', '马齿苋', '高瓜(1)', '高瓜(2)', '鱼腥草', '鱼腥草(份)', '鲜木耳(1)', '鲜木耳(2)', '鲜木耳(份)', '鲜粽叶', '鲜粽叶(袋)(1)', '鲜粽叶(袋)(2)', '鲜粽叶(袋)(3)', '鲜粽子叶', '鲜藕带(袋)', '鸡枞菌', '鹿茸菇(盒)', '黄心菜(1)', '黄心菜(2)', '黄白菜(1)', '黄白菜(2)', '黄花菜', '黑油菜', '黑牛肝菌', '黑牛肝菌(盒)', '黑皮鸡枞菌', '黑皮鸡枞菌(盒)', '龙牙菜']]\n",
    "alpha = 0.05\n",
    "n_c = column_data.shape[1]\n",
    "H = np.zeros(n_c)\n",
    "P = np.zeros(n_c)\n",
    "\n",
    "# 指定中文字体\n",
    "matplotlib.rcParams['font.sans-serif'] = ['SimHei']\n",
    "\n",
    "# 遍历每一列的检验结果，并添加到文档中\n",
    "for i in range(column_data.shape[1]):\n",
    "    h, p = stats.jarque_bera(column_data.iloc[:, i])\n",
    "    H[i] = h\n",
    "    P[i] = p\n",
    "    \n",
    "    if p < alpha:\n",
    "        result = \"不满足正态分布\"\n",
    "    else:\n",
    "        result = \"满足正态分布\"\n",
    "    \n",
    "    # 添加每一列的结果到文档中\n",
    "    doc.add_paragraph(f'第{i+1}列数据：{result}')\n",
    "    \n",
    "# 添加JB检验结果\n",
    "doc.add_heading('JB检验结果', level=2)\n",
    "doc.add_paragraph(f'h: {H}')\n",
    "doc.add_paragraph(f'p: {P}')\n",
    "\n",
    "# 保存文档\n",
    "doc.save('./results/蔬菜单品正态分布检验结果.docx')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
